package com.wxapp.common.base.caffeine;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.wxapp.common.constant.properties.CaffeineProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author lgl
 * @Description
 * @Datetime 2023/7/13 14:51
 * @Modified By
 */
@Configuration
@RequiredArgsConstructor
public class CaffeineConfig {
   private final CaffeineProperties caffeineProperties;

    @Bean
    public Cache<String, Object> caffeineCache() {
        return Caffeine.newBuilder()
                // 设置最后一次写入或访问后经过固定时间过期
                .expireAfterWrite(caffeineProperties.getExpire(), TimeUnit.SECONDS)
                // 初始的缓存空间大小
                .initialCapacity(caffeineProperties.getInitial())
                // 缓存的最大条数
                .maximumSize(caffeineProperties.getMaximum())
                .build();
    }
}
